<template>
  <basic-container>
    <avue-crud
      :option="option"
      :search.sync="search"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      :permission="permissionList"
      :before-open="beforeOpen"
      v-model="form"
      ref="crud"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @row-del="rowDel"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @sort-change="sortChange"
      @on-load="onLoad"
    >
    <template slot="menuLeft">
        <el-button
          type="danger"
          size="small"
          icon="el-icon-delete"
          plain
          @click="handleDelete"
          >删 除
        </el-button>
      </template>
    </avue-crud>
  </basic-container>
</template>
  
<script>
import {
  getList,
  getDetail,
  save,
  remove,
} from "@/api/rb-merchant/payinvoice/payinvoiceBuy";
import { mapGetters } from "vuex";

export default {
  components: {},
  data() {
    return {
      form: {},
      query: {},
      search: {},
      active: 0,
      openType: "", //打开dialog弹框的类型
      roleId: null, //角色id
      userType: "3",
      props: {
        //菜单权限数据 props
        label: "title",
        value: "key",
        disabled: this.isDisabledFn,
      },
      loading: true,
      excelBox: false,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      selectionList: [],
      option: {
        height: "calc(50vh - 50px)",
        calcHeight: 30,
        tip: false,
        border: true,
        index: true,
        selection: true,
        searchMenuSpan: 6,
        labelWidth: "150",
        columnBtn: false,
        addBtn: true,
        editBtn: true,
        viewBtn: true,
        delBtn: false,
        column: [
          {
            label: "主键",
            prop: "id",
            type: "input",
            addDisplay: false,
            editDisplay: false,
            viewDisplay: false,
            hide: true,
            overHidden: true,
          },
          {
            label: "购方类型",
            prop: "buyerType",
            type: "select",
            dataType: "string",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            span: 24,
            // search: true,
            dicUrl: "/api/blade-system/dict/dictionary?code=buyer_type",
            minWidth: 150,
            value: 2,
            rules: [
              {
                required: true,
                message: "请选择购方类型",
                trigger: "blur",
              },
            ],
            change: ({column, value}) => {
              if (value == 1) {
                const country = this.findObject(this.option.column, "country");
                country.display = false;
                const idNo = this.findObject(this.option.column, "idNo");
                idNo.label = '纳税人识别号';
              } else if (value == 2) {
                const country = this.findObject(this.option.column, "country");
                country.display = true;
                const idNo = this.findObject(this.option.column, "idNo");
                console.log(idNo, 'idNo');
                idNo.label = '证件号码';
              }
            }
          },
          {
            label: "购方名称",
            prop: "name",
            type: "input",
            minWidth: 150,
            span: 24,
            rules: [
              {
                required: true,
                message: "请输入购方名称",
                trigger: "blur",
              },
            ],
            overHidden: true,
          },
          {
            label: "国籍",
            prop: "country",
            type: "select",
            dataType: "string",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            span: 24,
            // search: true,
            dicUrl: "/api/blade-system/dict/dictionary?code=invoice_country",
            minWidth: 150,
            rules: [
              {
                required: true,
                message: "请选择国籍",
                trigger: "blur",
              },
            ],
          },
          {
            label: "证件类型",
            prop: "idType",
            type: "select",
            dataType: "string",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            span: 24,
            // search: true,
            dicUrl: "/api/blade-system/dict/dictionary?code=id_type",
            minWidth: 150,
            rules: [
              {
                required: true,
                message: "请选择证件类型",
                trigger: "blur",
              },
            ],
          },
          {
            label: "纳税人识别号",
            prop: "idNo",
            type: "input",
            minWidth: 150,
            span: 24,
            rules: [
              {
                required: true,
                message: "请输入纳税人识别号",
                trigger: "blur",
              },
            ],
            overHidden: true,
          },
          {
            label: "购方地址",
            prop: "address",
            type: "input",
            minWidth: 150,
            span: 24,
            rules: [
              {
                required: true,
                message: "请输入购方地址",
                trigger: "blur",
              },
            ],
            overHidden: true,
          },
          {
            label: "购方电话",
            prop: "phone",
            type: "number",
            minWidth: 150,
            span: 24,
            rules: [
              {
                required: true,
                message: "请输入购方电话",
                trigger: "blur",
              },
            ],
            overHidden: true,
          },
          {
            label: "开户银行",
            prop: "bankName",
            type: "input",
            minWidth: 150,
            span: 24,
            rules: [
              {
                required: true,
                message: "请输入开户银行",
                trigger: "blur",
              },
            ],
            overHidden: true,
          },
          {
            label: "银行账号",
            prop: "bankNo",
            type: "number",
            minWidth: 150,
            span: 24,
            rules: [
              {
                required: true,
                message: "请输入银行账号",
                trigger: "blur",
              },
            ],
            overHidden: true,
          },
          {
            label: "收票邮箱",
            prop: "email",
            type: "input",
            minWidth: 150,
            span: 24,
            rules: [
              {
                required: true,
                message: "请输入收票邮箱",
                trigger: "blur",
              },
            ],
            overHidden: true,
          },
          {
            label: "是否默认",
            prop: "isDefault",
            type: "radio",
            dataType: "string",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            span: 24,
            value: "1",
            dicData: [
              {
                dictKey: 0,
                dictValue: "否",
              },
              {
                dictKey: 1,
                dictValue: "是",
              },
            ],
            minWidth: 150,
            rules: [
              {
                required: true,
                message: "是否默认",
                trigger: "blur",
              },
            ],
          },
        ],
      },
      data: [],
    };
  },
  computed: {
    ...mapGetters(["permission", "selectMer"]),
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(",");
    },
  },
  created() {},
  methods: {
    rowSave(row, done, loading) {
      save({ ...row, rpiNo: this.selectMer.rpiNo }).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          done();
        },
        (error) => {
          loading();
          window.console.log(error);
        }
      );
    },
    rowUpdate(row, index, done, loading) {
      save({ ...row, rpiNo: this.selectMer.rpiNo }).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          done();
        },
        (error) => {
          loading();
          window.console.log(error);
        }
      );
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        });
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      this.openType = type;
      if (["edit", "view"].includes(type)) {
        getDetail(this.form.id).then((res) => {
          this.form = res.data.data;
        });
      }
      done();
    },
    /**
     * @func 菜单权限树形是否禁止
     * @desc dialog打开类型为 view 则禁止权限树
     * @param {String} openType - 打开类型
     * */
    isDisabledFn() {
      const _this = this;
      if (["view"].includes(_this.openType)) {
        return true;
      }
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.query, { rpiNo: this.selectMer.rpiNo })
      ).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    },
    sortChange(val) {
      if (val.order == "ascending") {
        //正序
        this.query.ascs = val.prop;
        this.query.descs = "";
      } else {
        //倒序
        this.query.descs = val.prop;
        this.query.ascs = "";
      }
      this.refreshChange();
    },
  },
};
</script>
  
  <style>
</style>